<!DOCTYPE html>
<html>
<head>
    <title>SourceDiff</title>
    <link href="bootstrap.min.css" rel="stylesheet" type="text/css">
    <link href="main_style.css" rel="stylesheet" type="text/css">
    <link href="air_style.css" rel="stylesheet" type="text/css">
    <script src="AIRAliases.js" type="text/javascript"></script>
</head>
<body onload="appLoad()">
<div class="wrapper">
    <div class="left">
        <h3>Original</h3>
    </div>
    <div class="right">
        <h3>Edit</h3>
    </div>
</div>
<div id="editButtons">
    <button class="btn" onclick="jumpToPrev()">
        <strong>Previous Edit</strong>
    </button>
    <button class="btn" onclick="jumpToNext()">
        <strong>Next Edit</strong>
    </button>
</div>
<div class="wrapper">
    <div class="air-left scroll" id="scrollLeft">
        <pre id="original_result"></pre>
    </div>
    <div class="air-right scroll" id="scrollRight">
        <pre id="edited_result"></pre>
    </div>
</div>
<script src="LineDiff.js"></script>
<script src="EditSet.js"></script>
<script src="LineUtils.js"></script>
<script src="Diff.js"></script>
<script src="DiffFormatter.js"></script>
<script src="LineFormatter.js"></script>
<script src="AnchorIterator.js"></script>
<script>
    function appLoad() {
        window.nativeWindow.maximize();

        air.NativeApplication.nativeApplication.addEventListener(air.InvokeEvent.INVOKE, onInvoke);

        var scrollLeft = document.getElementById('scrollLeft');
        var scrollRight = document.getElementById('scrollRight');

        scrollLeft.addEventListener('scroll', function() {
            scrollRight.scrollLeft = scrollLeft.scrollLeft;
            scrollRight.scrollTop = scrollLeft.scrollTop;
        });

        scrollRight.addEventListener('scroll', function() {
            scrollLeft.scrollLeft = scrollRight.scrollLeft;
            scrollLeft.scrollTop = scrollRight.scrollTop;
        });
    }

    function jumpToNext() {
        if (anchorIterator) {
            var line = anchorIterator.getNextEdit();
            if (line !== false) {
                jumpTo(line);
            }
        }
    }

    function jumpToPrev() {
        if (anchorIterator) {
            var line = anchorIterator.getPrevEdit();
            if (line !== false) {
                jumpTo(line);
            }
        }
    }

    function jumpTo(label) {
        window.location.hash = label;
    }

    function onInvoke(event) {
        var original = readFile(event.currentDirectory, event.arguments[0]);
        var edit = readFile(event.currentDirectory, event.arguments[1]);

        doDiff(original, edit);
    }

    function readFile(currentDirectory, filePath) {
        var file = currentDirectory.resolvePath(filePath);
        if (file.exists) {
            var fileStream = new air.FileStream();
            fileStream.open(file, air.FileMode.READ);
            var contents = fileStream.readUTFBytes(fileStream.bytesAvailable);
            fileStream.close();

            return contents;
        }

        return '';
    }

    var anchorIterator;

    function doDiff(original, edited) {
        var diff = new SourceDiff.Diff(true);
        var formatter = new SourceDiff.DiffFormatter(diff);

        var results = formatter.formattedDiff(original, edited);

        document.getElementById('original_result').innerHTML = results[0];
        document.getElementById('edited_result').innerHTML = results[1];

        anchorIterator = results[2];
    }
</script>
</body>
</html>